<template>
    <div class="login-wrap">
        <div class="ms-title">后台管理系统</div>
        <div class="ms-login">
            <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="0px" class="demo-ruleForm">
                <el-form-item prop="username">
                    <el-input v-model="ruleForm.username" placeholder="用户名"></el-input>
                </el-form-item>
                <el-form-item prop="password">
                    <el-input type="password" placeholder="密码" v-model="ruleForm.password" @keyup.enter.native="submitForm('ruleForm')"></el-input>
                </el-form-item>
                <div class="verifyNumber relative">
                    <i class="iconfont icon-ad80-copy"></i>
                    <!-- @keyup.enter="subParams" -->
                    <input type="text" style="width:150px; height:30px;margin-right:100px;" placeholder="输入验证码" v-model="ruleForm.verifyNumber" >
                    <div class="yan">
                    <img class="ImgStr"  :src='strbaser64' @click="initData">

                 </div>
          </div>
                <div class="login-btn">
                    <el-button type="primary" @click="submitBtn('ruleForm')">登录</el-button>
                </div>
            </el-form>
        </div>
    </div>
</template>

<script>
export default {
  data: function() {
    return {
      strbaser64: "",
      ruleForm: {
        username: "", //用户名
        password: "", //密码
        verifyNumber: "" //验证码
      },
      rules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" }
        ],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }],
        verifyNumber: [
          { required: true, message: "请输入验证码", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.initData();
  },
  methods: {
    //图片验证码
    async initData() {
      await this.$get({
        url: this.fn.chatApi + "/admin/getImageCode"
      }).then(res => {
        this.strbaser64 = "data:image/png;base64," + res.data.imageStringBase;
      });
    },
    //提交
    async submitForm(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          this.submitBtn();
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    async submitBtn() {
      let data = {
        imageCode: this.ruleForm.verifyNumber,
        loginName: this.ruleForm.username,
        passWord: this.ruleForm.password
      };

      let result = this.utils.encrypt(JSON.stringify(data), "YJ-YZGAMING@2018");

      // let Base64 = require('js-base64').Base64;
      // this.pw = Base64.encode(data1);
      await this.$post({
        url: this.fn.chatApi + "/admin/loginByPassWord",
        data: {
          ciphertext: result
        }
      }).then(res => {
        if (res.code == 200104 || res.code == 200105) {
          this.$notify.error({
            title: "错误",
            message: res.message
          });
        } else if (res.code == 111111) {
          this.$store.commit("setToken", res.data.adminToken);
          localStorage.setItem("ms_username", this.ruleForm.username);
          this.$router.push("/");
        }
      });
    }
  }
};
</script>

<style scoped>
.login-wrap {
  position: relative;
  width: 100%;
  height: 100%;
}
.ms-title {
  position: absolute;
  top: 50%;
  width: 100%;
  margin-top: -230px;
  text-align: center;
  font-size: 30px;
  color: #fff;
}
.ms-login {
  position: absolute;
  left: 50%;
  top: 50%;
  width: 300px;
  height: 210px;
  margin: -150px 0 0 -190px;
  padding: 40px;
  border-radius: 5px;
  background: #fff;
}
.login-btn {
  text-align: center;
}
.login-btn button {
  width: 100%;
  height: 36px;
}
.verifyNumber {
  position: relative;
  border-radius: 4px;
  background-color: #fff;
  border: 1px solid #bfcbd9;
  padding: 15px 10px;
  font-size: 14px;
  margin-bottom: 22px;
}
.verifyNumber input {
  border: none;
  outline: none;
  text-align: left;
}

.verifyNumber .yan {
  position: absolute;
  top: 5px;
  right: 15px;
  width: 140px;
  height: 50px;
  border-radius: 5px;
}
.ImgStr {
  width: 145px;
  height: 44px;
}
</style>